from configobj import ConfigObj
import yaml

class Config:
	def __init__(self, path='cubeassistant.cfg'):
		self.path = path
		#self.conf = ConfigObj(path) #os.path.expanduser('~/.cubeassistant.cfg')
		self.conf = yaml.load(open(path).read())
	
	def save(self):
		f = open(self.path,'w')
		f.write(yaml.dump(self.conf))
		#self.conf.write(f)
		f.close()

	def has(self,sect):
		if type(sect)==str:
			sect=sect.split('.')
		try:
			x=self.conf
			for s in sect:
				x=x[s]
			return True
		except:
			return False

	def set(self,sect,val):
		X=sect
		if type(sect)==str:
			sect=sect.split('.')

		x=self.conf
		for s in sect[:-1]:
			if not s in x:
				x[s]={}
			x=x[s]

		x[sect[-1]]=val

	def get(self,sect,default=None):
		if type(sect)==str:
			sect=sect.split('.')

		if not self.has(sect):
			return default

		x=self.conf
		for s in sect:
			x=x[s]

		return x	




